import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 连接池抽象类
 *
 * @author HHLJ
 * @date 2022/03/15
 */
public abstract class AbstractConnectionPool implements com.wangcanxuan.www.util.pool.MyDataSource {
    /**
     * 数据库url
     */
    private static String url;
    /**
     * 数据库用户名
     */
    private static String username;
    /**
     * 数据库密码
     */
    private static String password;

    /**
     * 传给子类：最大的空闲连接数
     */
    protected static Integer maxIdleConnections;
    /**
     * 传给子类：最大的正在使用的连接数
     */
    protected static Integer maxActiveConnections;
    /**
     * 传给子类：自动增加连接的连接数
     */
    protected static Integer incrementConnections;
    /**
     * 传给子类：从连接池中获取一个连接最大等待多少时间
     */
    protected static Integer timeToWait;

    //
    static{
        try(InputStream inputStream = AbstractConnectionPool.class.getClassLoader().getResourceAsStream("db.properties")) {
            Properties properties = new Properties();
            properties.load(inputStream);

            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("pwd");
            String driver = properties.getProperty("driver");

            maxIdleConnections = Integer.valueOf(properties.getProperty("maxIdleConnections"));
            maxActiveConnections = Integer.valueOf(properties.getProperty("maxActiveConnections"));
            incrementConnections = Integer.valueOf(properties.getProperty("incrementConnections"));
            timeToWait = Integer.valueOf(properties.getProperty("timeToWait"));
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Connection getConnection() throws SQLException {
        return getConnection(username, password);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return DriverManager.getConnection(url,username,password);
    }


}
